package com.example.sqldemo;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    Button addButton, updateButton, queryButton, deleteButton;
    EditText addInput1, addInput2, addInput3;
    EditText updateInput1, updateInput2, updateInput3;
    EditText queryInput1, queryInput2;
    EditText deleteInput1, deleteInput2;
    TextView showView;
    Button clearButton;
    private long mTime;
    private String msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addButton = findViewById(R.id.AddButton);
        updateButton = findViewById(R.id.UpdateButton);
        queryButton = findViewById(R.id.QueryButton);
        deleteButton = findViewById(R.id.DeleteButton);

        addInput1 = findViewById(R.id.AddInput1);
        addInput2 = findViewById(R.id.AddInput2);
        addInput3 = findViewById(R.id.AddInput3);

        updateInput1 = findViewById(R.id.UpdateInput1);
        updateInput2 = findViewById(R.id.UpdateInput2);
        updateInput3 = findViewById(R.id.UpdateInput3);

        queryInput1 = findViewById(R.id.QueryInput1);
        queryInput2 = findViewById(R.id.QueryInput2);

        deleteInput1 = findViewById(R.id.DeleteInput1);
        deleteInput2 = findViewById(R.id.DeleteInput2);

        clearButton = findViewById(R.id.buttonClear);
        showView = findViewById(R.id.ShowView);
        showView.setMovementMethod(ScrollingMovementMethod.getInstance());

        //add增
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String username = addInput1.getText().toString();
                final String password = addInput2.getText().toString();
                final String password2 = addInput3.getText().toString();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)||TextUtils.isEmpty(password2)) {
                    showView.setText("Input can't be empty!" + "\n" + showView.getText().toString());
                    Log.i("show", "empty!");
                }
                else if(!password.equals(password2)){
                    showView.setText("Passwords are unequal!" + "\n" + showView.getText().toString());
                    Log.i("show", "unequal!");
                }
                else{
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if(DBConnection.check(username)){
                                msg = "Username has been used!";
                                Log.i("show", "been used!");
                            }
                            else{
                                DBConnection.add(username,password);
                                msg = "Add '" + username + "' successfully!";
                                Log.i("show", "adding!");
                            }
                            showView.setText(msg + "\n" + showView.getText().toString());
                            addInput1.setText("");
                            addInput2.setText("");
                            addInput3.setText("");
                        }
                    }).start();
                }
            }
        });

        //update改
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String username = updateInput1.getText().toString();
                final String oldpassword = updateInput2.getText().toString();
                final String newpassword = updateInput3.getText().toString();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(oldpassword)||TextUtils.isEmpty(newpassword)) {
                    showView.setText("Input can't be empty!" + "\n" + showView.getText().toString());
                    Log.i("show", "empty!");
                }
                else{
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if(DBConnection.query(username,oldpassword)){
                                DBConnection.update(username,newpassword);
                                msg = "Update '" + username + "' successfully!";
                                Log.i("show", "updating!");
                            }
                            else{
                                msg = "Non-existent or wrong password!";
                                Log.i("show", "can't find!");
                            }
                            showView.setText(msg + "\n" + showView.getText().toString());
                            updateInput1.setText("");
                            updateInput2.setText("");
                            updateInput3.setText("");
                        }
                    }).start();
                }
            }
        });

        //query查
        queryButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String username = queryInput1.getText().toString();
                final String password = queryInput2.getText().toString();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)) {
                    showView.setText("Input can't be empty!" + "\n" + showView.getText().toString());
                    Log.i("show", "empty!");
                }
                else{
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if(DBConnection.query(username,password)){
                                msg = "Sign in successfully!";
                                Log.i("show", "succeeded!");
                            }
                            else{
                                msg = "Non-existent or wrong password!";
                                Log.i("show", "failed!");
                            }
                            showView.setText(msg + "\n" + showView.getText().toString());
                            queryInput1.setText("");
                            queryInput2.setText("");
                        }
                    }).start();
                }
            }
        });

        //delete删
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String username = deleteInput1.getText().toString();
                final String password = deleteInput2.getText().toString();
                if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)) {
                    showView.setText("Input can't be empty!" + "\n" + showView.getText().toString());
                    Log.i("show", "empty!");
                }
                else{
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            if(DBConnection.query(username,password)){
                                DBConnection.delete(username,password);
                                msg = "Delete '" + username + "' successfully!";
                                Log.i("show", "deleting!");
                            }
                            else{
                                msg = "Non-existent or wrong password!";
                                Log.i("show", "can't find!");
                            }
                            showView.setText(msg + "\n" + showView.getText().toString());
                            deleteInput1.setText("");
                            deleteInput2.setText("");
                        }
                    }).start();
                }
            }
        });

        //clear按钮清除log信息
        clearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showView.setText("");
            }
        });

        //线程模板
        /*new Thread(new Runnable() {
            @Override
            public void run() {
                do something
            }
        }).start();*/

        //隐藏顶部菜单栏
        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null) {
            actionBar.hide();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    //退出
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - mTime) > 2000) {
                Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
                //获取系统当前时间
                mTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}